package localTests;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.cookie.Cookie;


public class HttpTest {

	/**
	 * @param args
	 */
	String line;
	public static void main(String[] args) {
		String line;
		try{
			DefaultHttpClient client = new DefaultHttpClient();
			HttpPost post = new HttpPost("http://localhost:85/HttpTest.php");
			HttpPost postCookie = new HttpPost("http://localhost:85/CookieTest.php");
			StringEntity  se = new StringEntity("This is a test.");
			post.setEntity(se);
			System.out.println("Sending Post");
			HttpResponse response = client.execute(post);
			System.out.println("Post Sent");
			 List<Cookie> cookies = client.getCookieStore().getCookies();
	            if (cookies.isEmpty()) {
	                System.out.println("None");
	            } else {
	                for (int i = 0; i < cookies.size(); i++) {
	                    System.out.println("- " + cookies.get(i).toString());
	                }
	            }
			HttpEntity entity = response.getEntity();
			InputStream is = entity.getContent();
			BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
			line = reader.readLine();
			is.close();
			System.out.println(line);
			response = client.execute(postCookie);
			entity = response.getEntity();
			is = entity.getContent();
			reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
			line = reader.readLine();
			is.close();
			System.out.println(line);
			
		}
		catch(Exception e){
			System.out.println(e);
		}
	}

}
